package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.model.request.SgsGetCreditRequest;
import com.sgzn.sgpay.model.request.SgsIncreaseCreditRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.UserCreditVo;
import com.sgzn.sgpay.service.SgsGetCreditService;
import com.sgzn.sgpay.service.SgsIncreaseCreditService;
import com.sgzn.sgpay.web.controller.base.UserBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.CreditConsumeRequest;
import com.sgzn.sgpay.web.request.Impl.GetCreditRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 积分控制器
 * @author huangsenguang
 */
@Slf4j
@Controller
@RequestMapping("/user/credit")
public class CreditController extends UserBaseController{

    @Resource
    private SgsGetCreditService sgsGetCreditService;
    @Resource
    private SgsIncreaseCreditService sgsIncreaseCreditService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 查询积分
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<UserCreditVo> getCredit(GetCreditRequest request){
        super.setUserInfo(request);
        return sgsGetCreditService.getCredit(assembleGetRequest(request));
    }

    /**
     * 上分（消费）
     * @param request
     * @return
     */
    @RequestMapping("consume")
    @ResponseBody
    public SgsResultResponse<String> consume(CreditConsumeRequest request){
        super.setUserInfo(request);
        return this.sgsIncreaseCreditService.invoke(assembleConsumeRequest(request));
    }

    /**
     * 组装查询请求
     * @param request
     * @return
     */
    private SgsGetCreditRequest assembleGetRequest(GetCreditRequest request) {
        SgsGetCreditRequest sgsGetCreditRequest = new SgsGetCreditRequest();
        assembleCommonHandle.assembleUserRequest(request, sgsGetCreditRequest);
        return sgsGetCreditRequest;
    }

    /**
     * 组装上分请求
     * @param request
     * @return
     */
    private SgsIncreaseCreditRequest assembleConsumeRequest(CreditConsumeRequest request){
        SgsIncreaseCreditRequest target = new SgsIncreaseCreditRequest();
        target.setDeviceId(request.getDeviceId());
        target.setTrsAmount(request.getCost());
        assembleCommonHandle.assembleUserRequest(request, target);
        return target;
    }
}

